package cn.ahaiahaixx.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import cn.ahaiahaixx.entity.JsonMessage;
import cn.ahaiahaixx.exception.AppException;

/**
 * MyExceptionHandler全局错误处理器 ControllerAdvice注解表示是控制器拦截处理(app-面向切面编程)<br>
 * 
 * @author Aiden Pearce
 *
 */
@ControllerAdvice
@ResponseBody
public class MyExceptionHandler {

	private static final Logger log = LoggerFactory.getLogger(MyExceptionHandler.class);

	@ExceptionHandler(Exception.class)
	public JsonMessage handleExxception(Exception ex) {
		log.error("服务器发生错误", ex);

		// 处理应用程序自定义异常
		if (ex instanceof AppException) {
			AppException appException = (AppException) ex;
			return JsonMessage.getFail(appException.getCode(), appException.getMessage());
		}

		if (ex instanceof NoHandlerFoundException) {
			return JsonMessage.getFail(404, "资源不存在");
		}

		return JsonMessage.getFail("服务器繁忙，请稍后重试！");
	}

}
